<?php

namespace App\Repositories\Ca;

use App\Facade\AppContext;
use App\Models\BaseModel;
use App\Models\Ca\ComeWay;
use App\Models\Ca\ConsultationWay;
use App\Repositories\BaseRepository;
use App\Traits\RequestCache;
use App\Utils\Output;
use App\Utils\QueryHelper;

class ComeWayRepository extends BaseRepository
{
    use RequestCache;

    function getName($id)
    {
        return $this->requestCache('name_' . $id, function () use ($id) {
            return ComeWay::query()->where('id', '=', $id)->value('name');
        });
    }
    /**
     * 分页查询
     * @param array $params
     * @return Output
     */
    function adminPager(array $params)
    {
        extract($params);
        $query = $this->model->newQuery()->where('hospital_id', '=', AppContext::hospitalId());
        if (!empty($keyword)) {
            QueryHelper::like($query, 'name', $keyword);
        }
        $totalQuery = clone $query;
        $total = $totalQuery->count();
        $lis = $this->setParams($query, $params, ['weight' => 'desc', 'id' => 'desc']);
        return Output::pager($lis, $total);
    }

    function lis()
    {
        $query = ComeWay::query()->where('hospital_id', '=', AppContext::hospitalId());
        $query->select('id', 'name');
        return $query->orderByDesc('weight')->get();
    }

    function getModel(): BaseModel
    {
        return new ComeWay();
    }
}